package com.bootdo.screen.service.impl;

import com.bootdo.screen.domain.*;
import com.bootdo.screen.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Service
public class EnergyDataServiceImpl {


    @Autowired
    private TruefalseService truefalseService;

    @Autowired
    private SourcefalsenumService sourcefalsenumService;

    @Autowired
    private SourcetruenumService sourcetruenumService;
    @Autowired
    private TypefalsenumService typefalsenumService;
    @Autowired
    private  TypetruenumService typetruenumService;

    @Autowired
    private  BusinesstruenumService businesstruenumService;

    @Autowired
    private HotwordfalsenumService hotwordfalsenumService;

    @Autowired
    private HotwordtruenumService hotwordtruenumService;

    @Autowired
    private DataclassesService dataclassesService;


    public Map<String,Object> getAreaCount() {
        Map<String, Object>  map1 =  new HashMap<String, Object>();
        Long total = 0l;
        Long dibai = 0l;
        if (truefalse("能源数据总量")==0) {
            List<SourcefalsenumDO> re1 = sourcefalsenumService.list(null);
            if (!re1.isEmpty()) {
                SourcefalsenumDO sourcefalsenumDO = re1.get(re1.size() - 1);
                total = sourcefalsenumDO.getTotal();
                dibai = sourcefalsenumDO.getDibai();
            }
        } else {
            List<SourcetruenumDO> re1 = sourcetruenumService.list(null);
            if (!re1.isEmpty()) {
                SourcetruenumDO sourcetruenumDO = re1.get(re1.size() - 1);
                total = sourcetruenumDO.getTotal();
                dibai = sourcetruenumDO.getDibai();
            }
        }

        map1.put("dibai",dibai);
        map1.put("changpin",total-dibai);
        return  map1;
    }

    public Map<String,Object> getDataClasses() {

        List<DataclassesDO> datas = dataclassesService.list(null);
        Map<String, Object>  map1 =  new HashMap<String, Object>();
        map1.put("name","精华池");

        List<Map<String,Object>> list2 = new ArrayList<Map<String,Object>>();

        for (DataclassesDO data:datas) {
            Iterator<Map<String, Object>> l2it = list2.iterator();
            int f = 0;
            while (l2it.hasNext()) {
                Map<String, Object> l2nx = l2it.next();
                if (l2nx.get("name").equals(data.getLevel2())) {
                    f = 1;
                    Map<String, Object>  map3 =  new HashMap<String, Object>();
                    map3.put("name",data.getLevel3());
                    map3.put("value",data.getNum());
                    List<Map<String,Object>> l3 = (List<Map<String,Object>>)l2nx.get("children");
                    l3.add(map3);
                    l2nx.put("children",l3);
                    break;
                }
            }
            if (f == 0) {
                Map<String, Object>  map2 =  new HashMap<String, Object>();
                List<Map<String,Object>> list3 = new ArrayList<Map<String,Object>>();
                Map<String, Object>  map3 =  new HashMap<String, Object>();
                map3.put("name",data.getLevel3());
                map3.put("value",data.getNum());
                list3.add(map3);
                map2.put("name",data.getLevel2());
                map2.put("children",list3);
                list2.add(map2);
            }


        }




        map1.put("children",list2);


        return map1;

    }


    public List<Map<String,Object>> getHotWordCount(int limit) {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String, Object>  map =  new HashMap<String, Object>();
        map.put("limit",limit);
        map.put("offset",0);
        map.put("order","desc");
        map.put("sort","num");
        if (truefalse("搜索热词")==0) {
            List<HotwordfalsenumDO> re1 = hotwordfalsenumService.list(map);
            for (HotwordfalsenumDO re: re1) {
                Map<String, Object>  map1 =  new HashMap<String, Object>();
                map1.put("name",re.getWord());
                map1.put("value",re.getNum());
                list.add(map1);
            }
        } else {
            List<HotwordtruenumDO> re1 = hotwordtruenumService.list(map);
            for (HotwordtruenumDO re : re1) {
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put("id", re.getId());
                map1.put("name", re.getWord());
                map1.put("value", re.getNum());
                list.add(map1);
            }
        }
        return list;
    }


    public List<Map<String,Object>> getTypeCount() {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String, Object>  map =  new HashMap<String, Object>();
        map.put("limit",12);
        map.put("offset",0);

        Map<String, Object>  html =  new HashMap<String, Object>();
        Map<String, Object>  pdf =  new HashMap<String, Object>();
        Map<String, Object>   doc=  new HashMap<String, Object>();
        Map<String, Object>   xls=  new HashMap<String, Object>();
        Map<String, Object>   txt=  new HashMap<String, Object>();
        Map<String, Object>   ppt=  new HashMap<String, Object>();
        if (truefalse("入库量")==0) {
            List<TypefalsenumDO> re = typefalsenumService.list(map);


            for (TypefalsenumDO r:re) {
                html.put(String.valueOf(r.getDate().getTime()),r.getHtml());
                pdf.put(String.valueOf(r.getDate().getTime()),r.getPdf());
                doc.put(String.valueOf(r.getDate().getTime()),r.getDoc());
                xls.put(String.valueOf(r.getDate().getTime()),r.getXls());
                ppt.put(String.valueOf(r.getDate().getTime()),r.getPpt());
            }

        } else {
            List<TypetruenumDO> re = typetruenumService.list(map);
            for (TypetruenumDO r:re) {
                html.put(String.valueOf(r.getDate().getTime()),r.getHtml());
                pdf.put(String.valueOf(r.getDate().getTime()),r.getPdf());
                doc.put(String.valueOf(r.getDate().getTime()),r.getDoc());
                xls.put(String.valueOf(r.getDate().getTime()),r.getXls());
                txt.put(String.valueOf(r.getDate().getTime()),r.getTxt());
                ppt.put(String.valueOf(r.getDate().getTime()),r.getPpt());
            }

        }


        Map<String, Object>  html2 =  new HashMap<String, Object>();
        Map<String, Object>  pdf2 =  new HashMap<String, Object>();
        Map<String, Object>   doc2=  new HashMap<String, Object>();
        Map<String, Object>   xls2=  new HashMap<String, Object>();
        Map<String, Object>   txt2=  new HashMap<String, Object>();
        Map<String, Object>   ppt2=  new HashMap<String, Object>();

        html2.put("name","html");
        html2.put("data",html);

        pdf2.put("name","pdf");
        pdf2.put("data",pdf);

        doc2.put("name","doc");
        doc2.put("data",doc);

        xls2.put("name","xls");
        xls2.put("data",xls);

        ppt2.put("name","ppt");
        ppt2.put("data",ppt);

        list.add(html2);
        list.add(pdf2);
        list.add(doc2);
        list.add(xls2);
        list.add(txt2);
        list.add(ppt2);


        return list;
    }



    public List<Map<String,Object>> getBusinessCount() {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        List<BusinesstruenumDO> re1 = businesstruenumService.list(null);
        if (!re1.isEmpty()) {
            BusinesstruenumDO  businesstruenumDO= re1.get(re1.size() - 1);
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("name","石油信息数据量");
            map1.put("num",businesstruenumDO.getOil());

            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("name","天然气信息数据量");
            map2.put("num",businesstruenumDO.getGas());

            Map<String, Object> map3 = new HashMap<String, Object>();
            map3.put("name","煤炭信息数据量");
            map3.put("num",businesstruenumDO.getCoal());

            Map<String, Object> map4 = new HashMap<String, Object>();
            map4.put("name","电力信息数据量");
            map4.put("num",businesstruenumDO.getElectric());

            Map<String, Object> map5 = new HashMap<String, Object>();
            map5.put("name","新能源信息数据量");
            map5.put("num",businesstruenumDO.getAlternative());

            list.add(map1);
            list.add(map2);
            list.add(map3);
            list.add(map4);
            list.add(map5);
        }
        return list;
    }


    public  Long getEnergyTotal() {

        if (truefalse("能源数据总量")==0) {
            List<SourcefalsenumDO> re1 = sourcefalsenumService.list(null);
            if (!re1.isEmpty()) {
                SourcefalsenumDO sourcefalsenumDO = re1.get(re1.size() - 1);
                return sourcefalsenumDO.getTotal();
            }
        } else {
            List<SourcetruenumDO> re1 = sourcetruenumService.list(null);
            if (!re1.isEmpty()) {
                SourcetruenumDO sourcetruenumDO = re1.get(re1.size() - 1);
                return  sourcetruenumDO.getTotal();
            }
        }
        return 0L;
    }


    public List<Map<String,Object>> getSourceCount() {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

        if (truefalse("能源数据量")==0) {

            List<SourcefalsenumDO> re1 = sourcefalsenumService.list(null);

            if (!re1.isEmpty()) {
                SourcefalsenumDO sourcefalsenumDO = re1.get(re1.size() - 1);
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put("name", "互联网");
                map1.put("num", sourcefalsenumDO.getInternet());

                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("name", "外购");
                map2.put("num", sourcefalsenumDO.getTrade());

                Map<String, Object> map3 = new HashMap<String, Object>();
                map3.put("name", "邮件");
                map3.put("num", sourcefalsenumDO.getEmail());

                Map<String, Object> map4 = new HashMap<String, Object>();
                map4.put("name", "境外");
                map4.put("num", sourcefalsenumDO.getOutsource());

                Map<String, Object> map5 = new HashMap<String, Object>();
                map5.put("name", "微信");
                map5.put("num", sourcefalsenumDO.getWechat());

                list.add(map1);
                list.add(map2);
                list.add(map3);
                list.add(map4);
                list.add(map5);
            }



        } else {

            List<SourcetruenumDO> re1 = sourcetruenumService.list(null);



            if (!re1.isEmpty()) {
                SourcetruenumDO sourcetruenumDO = re1.get(re1.size() - 1);

                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put("name", "互联网");
                map1.put("num", sourcetruenumDO.getInternet());

                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("name", "外购");
                map2.put("num", sourcetruenumDO.getTrade());

                Map<String, Object> map3 = new HashMap<String, Object>();
                map3.put("name", "邮件");
                map3.put("num", sourcetruenumDO.getEmail());

                Map<String, Object> map4 = new HashMap<String, Object>();
                map4.put("name", "境外");
                map4.put("num", sourcetruenumDO.getOutsource());

                Map<String, Object> map5 = new HashMap<String, Object>();
                map5.put("name", "微信");
                map5.put("num", sourcetruenumDO.getWechat());

                list.add(map1);
                list.add(map2);
                list.add(map3);
                list.add(map4);
                list.add(map5);
            }

        }


        return list;

    }

    private int truefalse(String name){
        Map<String, Object>  map =  new HashMap<String, Object>();
        map.put("name",name);
        List<TruefalseDO> re = truefalseService.list(map);
        if (re.isEmpty()) {
            return 1;
        }
        return re.get(0).getValue();
    }
}
